package com.ponhu.ea2.goodsCenter.bms.api.controller;

import com.ponhu.ea2.common.basic.model.PagedResult;
import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.dto.PhMaterialDto;
import com.ponhu.ea2.dto.PhMaterialEditDto;
import com.ponhu.ea2.entity.Material;
import com.ponhu.ea2.enums.ResponseCodeEnum;
import com.ponhu.ea2.properties.GoodsProperties;
import com.ponhu.ea2.query.MaterialInfoQuery;
import com.ponhu.ea2.service.facade.PhMaterialInfoApiService;
import com.ponhu.ea2.support.exception.BusinessRuntimeException;
import com.ponhu.ea2.vo.PhAiPictureQueryVo;
import com.ponhu.ea2.vo.PhAiPictureVo;
import com.ponhu.ea2.vo.PhMaterialVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.ponhu.ea2.enums.ResponseCodeEnum.PARAMETER_IS_INCORRECT_800001;

/**
 * @author luyu
 * @create 2021-10-18 17:44
 */
@RestController
@RefreshScope
@RequestMapping("/material")
@Api(tags = "物料接口", value = "PhMaterialInfoController")
public class PhMaterialInfoController {

    @DubboReference(version = "1.0.0", check = false)
    private PhMaterialInfoApiService phMaterialInfoApiService;

    @Resource
    GoodsProperties goodsProperties;

    @ApiOperation(value = "查询物料列表")
    @PostMapping("/queryPhMaterialInfo")
    public ResponseBean<PagedResult<PhMaterialVo>> queryPhMaterialInfo(@RequestBody MaterialInfoQuery materialInfoQuery) {

        return ResponseBean.ok(phMaterialInfoApiService.queryPhMaterialInfo(materialInfoQuery));
    }

    @ApiOperation(value = "查询物料详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "物料编码",
                    dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getPhMaterialInfo")
    public ResponseBean<PhMaterialVo> queryPhMaterialInfo(@NotNull String materialCode) {
        // 查询物料信息
        ResponseBean<PhMaterialVo> phMaterialVo = phMaterialInfoApiService.getMaterialInfoByCode(materialCode);

        return phMaterialVo;
    }

    @ApiOperation(value = "根据型号id查询物料详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "型号id",
                    dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getPhMaterialInfoByModelId")
    public ResponseBean<PhMaterialVo> getPhMaterialInfoByModel(@NotNull Integer modelId) {

        return phMaterialInfoApiService.getMaterialInfoByModelId(modelId);
    }

    @ApiOperation(value = "新增物料信息")
    @PostMapping("/savePhMaterial")
    public ResponseBean savePhMaterial(@RequestBody @Validated PhMaterialDto phMaterialDto) {
        // 暂时去掉唯一校验
        /*Material material = phMaterialInfoApiService.checkPhMaterial(
                phMaterialDto.getBrandId(),
                phMaterialDto.getModel());
        if(ObjectUtils.isNotEmpty(material)){
            return ResponseBean.ok(material.getMaterialCode());
        }*/
        return phMaterialInfoApiService.savePhMaterial(phMaterialDto) ? ResponseBean.ok() : ResponseBean.error();
    }

    @ApiOperation(value = "编辑物料-回显详情信息")
    @ApiImplicitParam(name = "MaterialId", value = "物料id", dataType = "String", paramType = "query", required = true)
    @GetMapping("/getMaterialForEdit")
    public ResponseBean<PhMaterialVo> getMaterialInfo(@NotNull String MaterialId) {
        ResponseBean<PhMaterialVo> responseBean = phMaterialInfoApiService.getPhMaterialById(MaterialId);
        return responseBean;
    }

    @ApiOperation(value = "编辑保存物料相关信息")
    @PostMapping("/editPhMaterial")
    public ResponseBean editPhMaterial(@RequestBody @Validated PhMaterialEditDto phMaterialEditDto) {
        // 暂时去掉唯一校验
        /*Material material = phMaterialInfoApiService.checkPhMaterial(
                phMaterialEditDto.getBrandId(),
                phMaterialEditDto.getModelName());
        if(ObjectUtils.isNotEmpty(material) &&
                !material.getId().equals(phMaterialEditDto.getId())){
            return ResponseBean.ok(material.getMaterialCode());
        }*/
        return phMaterialInfoApiService.editPhMaterial(phMaterialEditDto) ? ResponseBean.ok() : ResponseBean.error();
    }

    /**
     * 包小检AI识图
     *
     * @param imgUrl
     * @return
     */
    @ApiOperation(value = "包小检AI识图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgUrl", value = "图片链接地址",
                    dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("/aiPictureClassify")
    public ResponseBean<PhAiPictureVo> aiPictureClassify(@RequestParam(value = "imgUrl") String imgUrl) {

        if (StringUtils.isEmpty(imgUrl))
            return ResponseBean.error(PARAMETER_IS_INCORRECT_800001.getMsg());

        if (BooleanUtils.isNotTrue(goodsProperties.getBxjOpen())) {
            return ResponseBean.ok(new PhAiPictureVo().setHasMatch(false).setErrMsg("AI识别功能已关闭，暂时不可使用"));
        }

        return ResponseBean.ok(phMaterialInfoApiService.aiPictureClassify(imgUrl));
    }

    /**
     * 查询包小检AI识图结果
     *
     * @param itemId
     * @param materialId
     * @param materialCode
     * @return
     */
    @ApiOperation(value = "查询包小检AI识图结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", value = "AI识图唯一标识", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "materialId", value = "物料ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "string", paramType = "query")
    })
    @GetMapping("/aiPictureQuery")
    public ResponseBean<PhAiPictureQueryVo> aiPictureQuery(
            @RequestParam(value = "itemId", required = false) String itemId,
            @RequestParam(value = "materialId", required = false) Long materialId,
            @RequestParam(value = "materialCode", required = false) String materialCode) {

        if (StringUtils.isEmpty(itemId) && ObjectUtils.isEmpty(materialId) && StringUtils.isEmpty(materialCode))
            throw new BusinessRuntimeException(PARAMETER_IS_INCORRECT_800001);

        return ResponseBean.ok(phMaterialInfoApiService.aiPictureQuery(itemId, materialId, materialCode));
    }

}
